#include "keeperoperate.h"

int visit_keeper(){
    int exist = 1 ;
    KEEPERLINK*keeperhead = NULL;
    KEEPERLINK*keeperend = NULL;
    keeperhead = creatkeeperhead(keeperhead);
    keeperend = creatkeeperlink(keeperhead);
    while(exist > 0){
        int choice = 0 ;
        printf("加入仓管员—————1\n删除仓管员—————2\n返回————3\n");
        scanf("%d",&choice);
        switch(choice){
            case 1:
                keeperend = creatkeeper(keeperhead,keeperend);
                break;
            case 2:
                deletkeeper();
                break;
            case 3:
                exist -- ;
                freekeeperlink();
                break;
            default:
                break;
        }

    }
}
KEEPERLINK*creatkeeperhead(keeperhead){
    keeperhead = (KEEPERLINK*)malloc(sizeof(struct Keeperlink));
    memset(keeperhead,0,sizeof(struct Keeperlink));
    keeperhead->next = NULL ;
    return keeperhead;
}

KEEPERLINK*creatkeeperlink(keeperhead){
    //断言
    assert(keeperhead!=NULL);
    
    KEEPERLINK*keeperend = keeperhead;
    //新的节点 添加到head的后面
    KEEPERLINK*keepernode = NULL;
    FILE *fp = NULL;
    char ch = 0 ;
    KEEPERLINK*temp = NULL ;
    temp = (KEEPERLINK*)malloc(sizeof(KEEPERLINK));
    fp = fopen("keeperlist.dat","rb");
    if(fp == NULL){
        perror("fopen");
        exit(1);
    }
    for(int i=0;(ch=fgetc(fp))!=EOF;i++){
    {
        keepernode = (KEEPERLINK*)malloc(sizeof(KEEPERLINK));
        memset(keepernode,0,sizeof(KEEPERLINK));
        //从文件中读到keeperlink 
        size_t wr = 0;
        wr = fread(temp,sizeof(struct Keeperlink),1,fp);
	    keepernode->keepernumber = temp->keepernumber ;
        keepernode->keeperpassword = temp->keeperpassword ;
        printf("%d号仓管员，账号%s：密码：%s\n",i,keepernode->keepernumber,keepernode->keeperpassword);
        //node 添加到head的后面
        keeperend->next = keepernode;
        keepernode->next = NULL;
        keeperend = keeperend->next;
    }
    free(temp);
    temp = NULL ;
    fclose(fp);
    return keeperend;
}

KEEPERLINK* creatkeeper(KEEPERLINK*keeperhead,KEEPERLINK*keeperend){
    unsigned int temp = 0 ;
    KEEPERLINK*keepernode = NULL ;
    keepernode = (KEEPERLINK*)malloc(sizeof(KEEPERLINK));
    memset(keepernode,0,sizeof(KEEPERLINK));
    for(int i=0;i<6;i++){
        temp = temp*10 + (keeperend->keepernumber[i]) ;
    }
    temp++;
    for(int i=5;i>=0;i--){
        (keepernode->keepernumber[i]) = temp%10 ;
        temp /= 10 ;
    }
    printf("新增仓管员的工号为：%s\n",keepernode->keepernumber);
    printf("请输入新增仓管员的密码\n");
    scanf("%s",keepernode.keeperpassword);
    keeperend->next = keepernode;
    keepernode->next = NULL;
    keeperend = keeperend->next;
    return keeperend;
}

KEEPERLINK*deletkeeper(KEEPERLINK*keeperhead){
    KEEPERLINK*keepernode = keeperhead ;
    KEEPERLINK*keepertemp = NULL ;
    char temp[6]={0};
    int error_signal = 1 ;
    print("请输入欲删除仓管员的工号：");
    scanf("%s",&temp);
    while(keepernode != NULL){
        if(strncmp((keepernode->next->keepernumber),temp,6)==0){
            error_signal--;
            keepertemp = keepernode->next ;
            keepernode->next = keepertemp->next
            free(keepertemp);
            keepertemp = NULL ;
            break;
        }
        keepernode = keepernode->next ;
    }
    if(error_signal > 0){
        printf("不存在该用户\n");
    }
    return keeperhead;
}

